"use strict"
const mvc = require("cosjs.mvc")
/*
活动奖励列表配置
活动信息配置统一在config中
*/

exports.index = function (){
    this.view = 'jqgrid';

    let pid = this.get('pid');
    let key = this.get('key');
    if(!pid){
        return this.error('活动ID不能为空');
    }
    if(!key){
        return this.error('奖励字段不能为空');
    }

    let data = {pid:pid,key:key,view:"branch/active",handle:"branch/active"}

    let keys = ['_id',"name","cid","AType"];
    let branch = this.branch;
    let configModel = this.model.branch('config',branch);
    return configModel.get(pid, keys).then(ret=>{
        if(!ret){
            return this.error('config not exist');
        }
        data['title']  = ret["name"];
        data['config'] = ret;
        return data;
    });
};

//获取道具配置列表
exports.page = function (){
    this.output = "json";
    let pid = this.get('pid');
    let key = this.get('key');
    if(!pid){
        return this.error('活动ID不能为空');
    }
    if(!key){
        return this.error('奖励字段不能为空');
    }
    let branch = this.branch;
    let configModel = this.model.branch('config',branch);
    return configModel.get(pid, key).then(ret=>{
        let item = ret||{};
        return {"rows":Object.values(item)};
    });
};


exports.save = function () {
    this.output = "json";
    let act = this.get('act');
    if(act == 'del'){
        return del.call(this);
    }
    else if (act=='edit') {
        return edit.call(this);
    }
    else{
        return add.call(this);
    }
}

function del() {
    let id = this.get('id','int'),
        pid = this.get('pid'),
        key = this.get('key');

    if ( !id || !pid || !key ) {
        return this.error('args empty');
    }

    let rkey = [key,id].join('.');
    let branch = this.branch;
    let configModel = this.model.branch('config',branch);
    return configModel.del(pid,rkey);
}

function add() {
    let pid = this.get('pid'),
        key = this.get('key');

    if ( !pid || !key ) {
        return this.error('args empty');
    }

    let info = {},format = mvc.format('branch/active/');
    for (let k in format) {
        if(format[k]['add']){
            info[k] = this.get(k, format[k]['type']) ;
        }
        if( format[k]['add'] > 1 && !info[k] ){
            let name = format[k]['name'] || k;
            return this.error(`${name}不能为空`);
        }
    }

    if(!info['ik']){
        return this.error('奖励编号不能为空');
    }

    let id = info['ik'];
    let field = [key,id].join('.');
    let branch = this.branch;
    let configModel = this.model.branch('config',branch);

    return configModel.get(pid,[key]).then(ret=>{
        if(!ret ){
            return this.error('active not exist');
        }
        if(ret['item'] && ret['item'][id] ){
            return this.error('active item exist');
        }
        return configModel.set(pid,field,info);
    });
}

function edit() {
    let id = this.get('id','int'),
        pid = this.get('pid'),
        key = this.get('key');
    if ( !id || !pid || !key ) {
        return this.error('args empty');
    }

    let info = {},format = mvc.format('branch/active/');
    for (let k in format) {
        if(format[k]['edit']){
            info[k] = this.get(k, format[k]['type']) ;
        }
        if( format[k]['edit'] > 1 && !info[k] ){
            let name = format[k]['name'] || k;
            return this.error(`${name}不能为空`);
        }
    }

    info['ik'] = id;
    let field = [key,id].join('.');
    let branch = this.branch;
    let configModel = this.model.branch('config',branch);
    return configModel.get(pid,[field]).then(ret=>{
        if(!ret){
            return this.error('active not exist');
        }
        if( !ret[key] || !ret[key][id] ){
            return this.error('active item not exist');
        }
        return configModel.set(pid,field,info);
    });
}
